﻿using IQIGame.Onigao.GameAOT;
using LFloatMath.Math;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class TotemViewComponent : TComponent
    {
        private TotemView _totem;
        public GameObject gameObject { get; private set; }
        private string _effectName;

        private uint _effectID;
        
        protected override void OnDispose()
        {
            if (this._effectID > 0)
            {
                this._totem.GetComponent<EffectComponent>(ETComponentType.EffectComponent)?.RemoveEffect(this._effectID);
            }
            BattleClientUtil.SafeDestroy(this.gameObject);
            this.gameObject = null;
        }

        public override void OnCreate(TEntity entity)
        {
            this._totem = entity as TotemView;
        }

        // 暂时不处理二次移动，需要了再加update
        public void Initialize(string effectName)
        {
            this._effectName = effectName;
        }
        
        public void InitializeView()
        {
            if (!this.gameObject)
            {
                this.gameObject = new GameObject("Totem");
            }
            this.gameObject.transform.position = this._totem.pos.ToVector3XZ();
            var rEffectComp = this._totem.GetComponent<EffectComponent>(ETComponentType.EffectComponent);
            var rEffectPath = rEffectComp.GetRoleEffectPath(this._effectName);
            var rBindComp = this._totem.GetComponent<BattleViewBindPointComponent>(ETComponentType.BattleViewBindPointComponent);
            var rInitParam = new EffectInitParam(rEffectPath, rBindComp.GetBindPoint(0), Vector3.zero, Quaternion.identity, isLoop: true, vfxSpace: EVFXSpace.Local);
            this._effectID = rEffectComp.AddEffect(rInitParam);
        }
    }
}